// 引入富文本解析插件
var WxParse = require('../../../wxParse/wxParse.js');
// 引入公用的自定义函数
var $ = require('../../../utils/js.js');
var img_gy_src = $.img_gy_src;

Page({
  // 页面的初始数据
  data: {
    // 图片路径的公用部分
    img_gy_src: img_gy_src,

    // 规则数据
    rule_data: [{
      title: "开团/参团",
      content: "支付成功后才算开团/参团成功"
    }, {
      title: "分享拼团给好友",
      content: "邀请参团"
    }, {
      title: "拼团成功",
      content: "参团人数达到成团人数即拼团成功，拼不成则退款"
    }],

    // 是否显示弹框
    tan_kuang: false,
    // 是否显示规则弹框
    tk_rule: false,
    // 弹框中的规则数据
    tk_rule_data: [{
      content: "拼团订单支付成功后视为开团/参团成功"
    }, {
      content: "必须在活动时间内邀请足够多的好友参团，拼团才算成功"
    }, {
      content: "若未在活动时间内完成拼团，则将自动退款至支付账户"
    }, {
      content: "如对活动有任何疑问，请联系本店微信客服或致电咨询"
    }],

    // 是否显示评论弹框
    ping_lun: false,
    // 是否显示盒子弹出框
    tk_box: false,

    // 评论的内容
    pl_value: "",

    // 生成的小程序码
    xcxewm: "",

    // 是否刷新页面
    shua_xin: false,
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    this.setData({
      goods_id: options.id
    })
    // 获取商品详情
    this.get_goods_data();
  },
  // 生命周期函数--监听页面显示
  onShow: function() {
    if (this.data.shua_xin) {
      // 获取商品详情
      this.get_goods_data();
    } else {
      this.setData({
        shua_xin: true
      })
    }
  },
  //商品详情函数
  get_goods_data() {
    var goods_id = this.data.goods_id;
    $.ajax({
      url: "/goods/detail",
      method: "GET",
      data: {
        goods_id: goods_id
      },
      success: (res) => {
        var data = res.data.data;
        console.log("商品信息", data);
        if (data) {
          // 调用富文本函数
          WxParse.wxParse('fu_wen_ben', 'html', data.good_content, this);
          this.setData({
            goods_data: data,
          })
        }
      }
    });
  },
  // 点击收藏商品按钮时触发
  shou_cang_fun() {
    var is_sc = this.data.goods_data.is_sc;
    var status = is_sc == 0 ? 1 : 2;
    $.ajax_get({
      url: "/goods/collect",
      data: {
        goods_id: this.data.goods_id,
        status: status
      },
      success: (res) => {
        $.ti_shi({
          title: res.data.msg
        })
        if (res.data.code) {
          // 获取商品详情
          this.get_goods_data();
        }
      }
    })
  },
  //店铺收藏
  dpsc() {
    $.ajax_get({
      url: "/shops/collect",
      data: {
        shop_id: this.data.goods_data.shop_id,
        status: this.data.goods_data.shop_collect == 0 ? 1 : 2
      },
      success: (res) => {
        $.ti_shi({
          title: res.data.msg
        })
        if (res.data.code == 1) {
          // 获取商品详情
          this.get_goods_data();
        }
      },
    })
  },
  //进店逛逛
  jindiangg() {
    $.open('/pages/shang_quan/dian_pu_xiang_qing/dian_pu_xiang_qing?id=' + this.data.goods_data.shop_id)
  },
  // 点击详细规则时触发
  rule_show_fun() {
    this.setData({
      tk_rule: true,
      tan_kuang: true,
    })
  },
  // 关闭弹框时调用
  tk_back_fun() {
    this.setData({
      tan_kuang: false,
      tk_rule: false,
      ping_lun: false,
      tk_box: false,
    })
  },
  // 点击评论时触发
  pl_fun() {
    this.setData({
      ping_lun: true,
      tan_kuang: true,
      tk_box: false,
    })
  },
  //评论value变化
  pinglunvalue(e) {
    this.setData({
      pl_value: e.detail.value
    })
  },
  // 点击评论弹框中的发送时触发
  pl_fs_fun() {
    let data = {
      goods_id: this.data.goods_id,
      content: this.data.pl_value
    }
    $.ajax_get({
      url: "/goods/add_comment",
      data: data,
      success: (res) => {
        $.ti_shi({
          title: res.data.msg
        })
        if (res.data.code == 1) {
          this.setData({
            pl_value: "",
          })
          // 获取商品详情
          this.get_goods_data();
        }
        this.tk_back_fun();
      },
    })
  },
  //评论删除
  plsc(e) {
    let id = e.currentTarget.dataset.id
    $.ajax_get({
      url: "/goods/comment_del",
      data: {
        id: id
      },
      success: (res) => {
        $.ti_shi({
          title: res.data.msg
        })
        if (res.data.code == 1) {
          // 获取商品详情
          this.get_goods_data();
        }
        this.tk_back_fun();
      },
    })
  },
  // 根据prom_type获取url
  get_url(prom_type) {
    switch (prom_type) {
      case 0:
        return "pages/goods/xq/xq"
        break;
      case 1:
        return "pages/goods/xq_qiang_gou/xq_qiang_gou"
        break;
      case 2:
        return "pages/goods/xq_kan_jia/xq_kan_jia"
        break;
      case 3:
        return "pages/goods/xq_pin_tuan/xq_pin_tuan"
        break;
      case 4:
        return "pages/goods/xq_qing_cang/xq_qing_cang"
        break;
      case 5:
        return "pages/goods/xq_yu_yue/xq_yu_yue"
        break;
    }
  },
  // 点击立即购买时触发
  box_show_fun() {
    // 如果有了小程序码
    if (this.data.xcxewm) {
      this.setData({
        tk_box: true,
        tan_kuang: true,
      })
    } else {
      let page = this.get_url(this.data.goods_data.prom_type);
      let scene = this.data.goods_id;
      var xcxewm = "https://www.yifale.com/api/token/qr_codedata?scene=" + scene + "&page=" + page
      this.setData({
        xcxewm: xcxewm,
        tk_box: true,
        tan_kuang: true,
      })
    }
  },
  // 点击小程序码时触发
  yu_lan_fun(e) {
    var src = e.currentTarget.dataset.src;
    $.yu_lan(src, [src]);
  },
  //前往聊天
  liao_tian(e) {
    $.deng_lu(() => {
      var receive_id = this.data.goods_data.shop_info.user_id;
      var title = this.data.goods_data.shop_info.user_nick;
      $.open("/pages/my/liao_tian_content/liao_tian_content?user_id=" + getApp().globalData.userinfo.id + '&receive_id=' + receive_id + "&title=" + title);
    })
  },
  // 自定义函数   结束
  // 用户点击右上角分享
  onShareAppMessage: function () {

  },
})